<?php
/**
 * Author: xx.com
 */ 
namespace Api\Controller;
use Think\Page;
class PromotionController extends BaseController {
    
    /**
     * 析构流函数
     */
    public function  __construct() {   
        parent::__construct();    
    }
    
	/**
	 * 申请优惠券
	 */
    public function applyCoupon(){
		$user_id = $this->user_id;
		// 查找身上的优惠券
		$where_coupon['user_id'] = $user_id;
		$coupon = M('reg_user')->where($where_coupon)->find();
		if($coupon){
			$this->returnInfo('-1','已存在优惠券');
		}else{
			// 新增优惠券
			$add_coupon['order_id'] = 0;
			$add_coupon['user_id'] = $user_id;
			$add_coupon['add_time'] = time();
			$add_coupon['save_time'] = time();
			$coupon_res = M('reg_user')->add($add_coupon);
			if($coupon_res){
				$this->returnInfo('1','领取成功');
			}else{
				$this->returnInfo('-1','领取失败');
			}
		}
	}
	
	/**
	 * 申请优惠券
	 */
	public function couponState(){
		$user_id = $this->user_id;
		// 查找身上的优惠券
		$where_coupon['user_id'] = $user_id;
		$coupon = M('reg_user')->where($where_coupon)->find();
		$this->returnInfo('1','查询成功',['coupon'=>$coupon]);
		
	}
	
	public function getGood()
	{
	    $id = I('id');
	    $page = I('p') ? I('p') : I('page',1);
		$reg_goods = M('reg_goods')->order('id desc')->select();
		$in_goods = '';
		foreach($reg_goods as $k => $v){
			$in_goods .= $v['goods_id'].",";
		}
	    $goods_where = array('is_on_sale' => 1,'prom_id'=>0,'prom_type'=>0,'goods_id'=>['IN',$in_goods]);
	    if( $id )
	    {
	        $goods_where['cat_id'] = $id;
	    }
	    $orderBy = array('sort' => 'desc');
	    $new_goods = M('goods')
	        ->field('goods_id,goods_name,goods_remark,shop_price,market_price,original_img')
	        ->where($goods_where)
	        ->page($page,20)
	        ->order($orderBy)
	        ->select();
	    foreach($new_goods as $k => &$v)
	    {
			$v['shop_price'] = M('reg_goods')->where(['goods_id'=>$v['goods_id']])->find()['price'];
	        $v['original_img'] = parseMediaUrl( $v['original_img'] );
	    }
	    $this->returnInfo('1','获取成功',$new_goods);
	}
}